package fr.web325.dao;

import fr.web325.utils.QueryRunnerUtils;
import fr.web325.bean.Notice;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class NoticeDao {
    public Class<Notice> NoticeClass = Notice.class;


    // 查询所有订单列表
    public List<Notice> getNoticeList() throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());// 查询并返回Notice对象
        return qr.query("select * from notice", new BeanListHandler<>(NoticeClass));
    }

    // 根据ID查询书本
    public Notice getNoticeById(String id) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());// 查询并返回Notice对象
        return qr.query("select * from notice where n_id=?", new BeanHandler<>(NoticeClass), id);
    }

    // 根据Notice类插入
    public Boolean addNotice(Notice notice) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("insert into notice(title,details,n_time) values (?,?,?)", notice.getTitle(), notice.getDetails(), notice.getN_time()) > 0;
    }

    // 根据ID修改公告
    public Boolean editNotice(Notice notice) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("update notice set title=?,details=?,n_time=? where n_id=?", notice.getTitle(), notice.getDetails(), notice.getN_time(), notice.getN_id()) > 0;
    }

    // 根据id删除Notice
    public Boolean removeNoticeById(String id) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("delete from notice where n_id=?", id) > 0;
    }
}
